package net.nothinglost.renotification;

/*
    Copyright (C) 2011, Carl Reinke
    Copyright (C) 2011, Jeff Moyer <phro@alum.wpi.edu>

    This file is part of Renotification.

    Renotification is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Renotification is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Renotification.  If not, see <http://www.gnu.org/licenses/>.
*/

import android.content.Intent;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

public class Preferences extends PreferenceActivity
{
    Preference.OnPreferenceChangeListener serviceEnabledPreferenceListener = new OnPreferenceChangeListener()
    {
        @Override
        public boolean onPreferenceChange( Preference preference, Object newValue )
        {
            if ((Boolean) newValue)
                startService();

            return true;
        }
    };
    
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        
        addPreferencesFromResource(R.xml.preferences);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        
        if (preferences.getBoolean(getString(R.string.service_enabled_key), true))
            startService();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        
        /* attach service-enabled listener */
        Preference serviceEnabledPreference = findPreference(getString(R.string.service_enabled_key));
        serviceEnabledPreference.setOnPreferenceChangeListener(serviceEnabledPreferenceListener);
    }
    
    private void startService()
    {
        startService(new Intent(getApplicationContext(), NotificationService.class));
    }
}